package com.mandao.concurrency.example.atomic;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 */
@Slf4j
public class SynchronizedExample1 {

    //修饰类
    public static void test0(int j){
        synchronized (SynchronizedExample1.class){
            for (int i = 0;i<10;i++){
                log.info("test0-{}",j,i);
            }
        }
    }

    //修饰代码块
    public void test1(){
        synchronized(this){
            for (int i = 0;i<10;i++){
                log.info("test1-{}",i);
            }
        }
    }

    //修饰方法
    public synchronized void test2(){
        for (int i = 0;i<10;i++){
            log.info("test2-{}",i);
        }
    }

    public static void main (String[] args){
        SynchronizedExample1 example1 = new SynchronizedExample1();
        SynchronizedExample1 example2 = new SynchronizedExample1();
        ExecutorService executorService = Executors.newCachedThreadPool();
        //线程池 两个线程同时调用test1
        executorService.execute(()->{
            example1.test2();
        });
        executorService.execute(()->{
            example2.test2();
        });
        executorService.execute(()->{
            example2.test2();
        });
    }
}
